package com.OrderingSystem.controller.seller;


import com.OrderingSystem.pojo.OrderMaster;
import com.OrderingSystem.pojo.ProductCategory;
import com.OrderingSystem.service.IOrderMasterService;
import com.OrderingSystem.service.impl.PushMessageServiceImpl;
import com.OrderingSystem.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Chenlin
 * @since 2025-08-13
 */
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/seller/order-master")
@ApiResponses({
        @ApiResponse(code = 200, message = "请求成功",response = Result.class),
        @ApiResponse(code = 403, message = "没有权限"),
        @ApiResponse(code = 404, message = "资源不存在"),
        @ApiResponse(code = 500, message = "服务器错误")
})
public class OrderMasterController {

    @Autowired
    IOrderMasterService orderMasterService;

    @Autowired
    PushMessageServiceImpl pushMessageService;

    //查询所有订单
    @GetMapping("/list")
    public Result selectAllOrderMaster(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                       OrderMaster orderMaster) {
        PageHelper.startPage(pageNum, pageSize);
        List<OrderMaster> orderMasters = orderMasterService.selectAllOrderMaster(pageNum, pageSize,orderMaster);
        PageInfo<OrderMaster> pageInfo = new PageInfo<>(orderMasters);
        return Result.success(pageInfo);
    }

    //根据订单id取消订单
    @PostMapping("/cancel")
    public Result cancelOrder(@RequestParam("orderId") Integer orderId,@RequestParam("buyerOpenid") String buyerOpenid) {
        orderMasterService.cancelOrder(buyerOpenid, String.valueOf(orderId));
        //推送消息
        pushMessageService.orderStatus(orderId);

        return Result.success("取消订单成功");
    }
}
